package net.dgg.zz.credential.certificateWarning.service;

import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.credential.certificateWarning.dao.CertificateWarningDao;
import net.dgg.zz.credential.certificateWarning.dto.CertificateWarningDto;
import net.dgg.zz.credential.warningConfigure.constant.WarningType;
import net.dgg.zz.credential.warningConfigure.service.SystemConfigureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsx
 * @Title: CertificateWarningService
 * @ProjectName zz-sc-zs
 * @Description: 证书预警服务类
 * @date 2018/8/24 8:31
 */
@Service
public class CertificateWarningService {
    @Autowired
    private CertificateWarningDao certificateWarningDao;
    @Autowired
    private SystemConfigureService configureService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private SessionManager sessionManager;


    /*
     * @Author zsx
     * @Date 2018-08-22 06:08:26
     * @Description 分页获取到期预警数据
     */
    public List<CertificateWarningDto> queryExpireWarningByWithPage(Map<String, Object> params) {
        checkPermissions(params);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        params.put("overdueType", CredentialConstant.CREDENTIAL_OVER_DUE_TYPE_CREDENTIAL);
        params.put("outgoingReason", CredentialConstant.OUT_GOING_REASON_TALENT_RETURN);
        //如果查看的那个事业部则获取事业部预警值 否则综合
        if (StringUtils.isNotEmpty(params.get("signOrgId"))) {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "synthesize_sort");//排序的列
            }
            Map<String, Object> map = new HashMap<>();
            map.put("signOrgId", params.get("signOrgId"));
            map.put("warningType", CredentialConstant.GENERAL_CREDENTIAL);
            map.put("warningName", WarningType.CERTIFICATE_EXPIRE_WARNING);
            Map<String, Object> warningMap = configureService.querySingleWarning(map);
            Integer warningValue = warningMap.get("configValue") == null ? 0 : Integer.parseInt(warningMap.get("configValue").toString());
            params.put("warningTime", DateUtils.getFewDaysAgo(warningValue));
            return certificateWarningDao.selectExpireWarningByWithPage(params);
        } else {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "AG.synthesize_sort");//排序的列
            }
            return certificateWarningDao.selectAllExpireWarningByWithPage(params);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-24 08:08:30
     * @Description 分页获取证书使用预警
     * @param params
     * @return List<CertificateWarningDto>
     */
    public List<CertificateWarningDto> queryAffiliatedWarningByWithPage(Map<String, Object> params) {
        checkPermissions(params);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        params.put("overdueType", CredentialConstant.CREDENTIAL_OVER_DUE_TYPE_AFFILIATED);
        params.put("outgoingReason", CredentialConstant.OUT_GOING_REASON_TALENT_RETURN);
        if (StringUtils.isNotEmpty(params.get("signOrgId"))) {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "synthesize_sort");//排序的列
            }
            Map<String, Object> map = new HashMap<>();
            map.put("signOrgId", params.get("signOrgId"));
            map.put("warningType", CredentialConstant.GENERAL_CREDENTIAL);
            map.put("warningName", WarningType.CERTIFICATE_HANG_UP_WARNING);
            Map<String, Object> warningMap = configureService.querySingleWarning(map);
            Integer warningValue = warningMap.get("configValue") == null ? 0 : Integer.parseInt(warningMap.get("configValue").toString());
            params.put("warningTime", DateUtils.getFewDaysAgo(warningValue));
            return certificateWarningDao.selectAffiliatedWarningByWithPage(params);
        } else {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "AG.synthesize_sort");//排序的列
            }
            return certificateWarningDao.selectAllAffiliatedWarningByWithPage(params);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-24 10:08:12
     * @Description 分页获取证书未使用预警
     * @param params
     * @return List<CertificateWarningDto>
     */
    public List<CertificateWarningDto> queryUnusedWarningByWithPage(Map<String, Object> params) {
        checkPermissions(params);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        params.put("generalStatus", CredentialConstant.GENERAL_CREDENTIAL_STATUS_NO_USE);
        params.put("libraryStatus", CredentialConstant.GENERAL_CREDENTIAL_LIBRARY_STATUS_IN);
        if (StringUtils.isNotEmpty(params.get("signOrgId"))) {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "gc.last_user_time");//排序的列
            }
            Map<String, Object> map = new HashMap<>();
            map.put("signOrgId", params.get("signOrgId"));
            map.put("warningType", CredentialConstant.GENERAL_CREDENTIAL);
            map.put("warningName", WarningType.CERTIFICATE_NOT_USED_WARNING);
            Map<String, Object> warningMap = configureService.querySingleWarning(map);
            Integer warningValue = warningMap.get("configValue") == null ? 0 : Integer.parseInt(warningMap.get("configValue").toString());
            params.put("warningTime", DateUtils.getFewDaysAgo(-warningValue));
            return certificateWarningDao.selectUnusedWarningByWithPage(params);
        } else {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "AG.last_user_time");//排序的列
            }
            return certificateWarningDao.selectAllUnusedWarningByWithPage(params);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-24 02:08:18
     * @Description 分页获取企业合同到期预警
     * @param params
     * @return List<CertificateWarningDto>
     */
    public List<CertificateWarningDto> queryEnterpriseContractWarningByWithPage(Map<String, Object> params) {
        checkPermissions(params);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        params.put("overdueType", CredentialConstant.CREDENTIAL_OVER_DUE_TYPE_COMPANY);
        params.put("outgoingReason", CredentialConstant.OUT_GOING_REASON_TALENT_RETURN);
        if (StringUtils.isNotEmpty(params.get("signOrgId"))) {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "gc.company_contract_expire_time");//排序的列
            }
            Map<String, Object> map = new HashMap<>();
            map.put("signOrgId", params.get("signOrgId"));
            map.put("warningType", CredentialConstant.GENERAL_CREDENTIAL);
            map.put("warningName", WarningType.COMPANY_CONTRACT_WARNING);
            Map<String, Object> warningMap = configureService.querySingleWarning(map);
            Integer warningValue = warningMap.get("configValue") == null ? 0 : Integer.parseInt(warningMap.get("configValue").toString());
            params.put("warningTime", DateUtils.getFewDaysAgo(warningValue));
            return certificateWarningDao.selectEnterpriseContractWarningByWithPage(params);
        } else {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "AG.company_contract_expire_time");//排序的列
            }
            return certificateWarningDao.selectAllEnterpriseContractWarningByWithPage(params);
        }
    }


    /*
     * @Author zsx
     * @Date 2018-08-24 02:51:18
     * @Description 分页获取人才合同到期预警
     * @param params
     * @return List<CertificateWarningDto>
     */
    public List<CertificateWarningDto> queryTalentContractWarningByWithPage(Map<String, Object> params) {
        checkPermissions(params);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        params.put("overdueType", CredentialConstant.CREDENTIAL_OVER_DUE_TYPE_TALENTS);
        params.put("outgoingReason", CredentialConstant.OUT_GOING_REASON_TALENT_RETURN);
        if (StringUtils.isNotEmpty(params.get("signOrgId"))) {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "gc.talent_contract_expire_time");//排序的列
            }
            Map<String, Object> map = new HashMap<>();
            map.put("signOrgId", params.get("signOrgId"));
            map.put("warningType", CredentialConstant.GENERAL_CREDENTIAL);
            map.put("warningName", WarningType.TALENT_CONTRACT_WARNING);
            Map<String, Object> warningMap = configureService.querySingleWarning(map);
            Integer warningValue = warningMap.get("configValue") == null ? 0 : Integer.parseInt(warningMap.get("configValue").toString());
            params.put("warningTime", DateUtils.getFewDaysAgo(warningValue));
            return certificateWarningDao.selectTalentContractWarningByWithPage(params);
        } else {
            if (null == params.get("orderColumn")) {
                params.put("orderDir", "ASC");//排序方向
                params.put("orderColumn", "AG.talent_contract_expire_time");//排序的列
            }
            return certificateWarningDao.selectAllTalentContractWarningByWithPage(params);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-27 11:08:12
     * @Description 判断权限
     */
    private void checkPermissions(Map<String, Object> params) {
        if (params.containsKey("isAll") && params.get("isAll").equals("0")) {
            params.put("signOrgId", commonService.findUserSyb(sessionManager.getCurrentUser()));
        }
    }


}
